package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.MccType;
import com.eeepay.eeepay_v2.model.ProductInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Ching on 2016/4/6.
 * 进件业务产品Adapter
 */
public class MerTypeAdapter extends BaseAdapter {

    private boolean isFirst = true;

    // 用于记录每个RadioButton的状态，并保证只可选一个
    HashMap<String, Boolean> states = new HashMap<String, Boolean>();

    private List<MccType> list = new ArrayList<>();

    private Context mContext;
    private Boolean isMerType = true;
    private MccType mccType = null;
    private String content;


    public MerTypeAdapter(Context mContext, boolean isMerType) {
        this.mContext = mContext;
        this.isMerType = isMerType;
    }

    public void addAll(List<MccType> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_simple_list, parent, false);
            holder = new ViewHolder();
            holder.typeTv = (TextView) convertView.findViewById(R.id.tv_simple_list);
            convertView.setTag(holder);
        }

        if (isMerType) {
            content = list.get(position).getSysName();
        } else {
            content = list.get(position).getSysValue() + "   " + list.get(position).getSysName();
        }
        if (content != null)
            holder.typeTv.setText(content);

//        final RadioButton rbtn = (RadioButton) convertView.findViewById(R.id.rbtn_simple_list);
//        holder.rbtn = rbtn;
//        if (isFirst && position == 0) {//默认第一个被选中
//            isFirst = false;
//            holder.rbtn.setChecked(true);
//            states.put(String.valueOf(position), rbtn.isChecked());
//        }
//
//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                // 重置，确保最多只有一项被选中
//                for (String key : states.keySet()) {
//                    states.put(key, false);
//                }
//                states.put(String.valueOf(position), true);
//                MerTypeAdapter.this.notifyDataSetChanged();
//
//            }
//        });
//        holder.rbtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
//        boolean flag = false;
//        if (states.get(String.valueOf(position)) == null || states.get(String.valueOf(position)) == false) {
//            flag = false;
//            states.put(String.valueOf(position), false);
//        } else {
//            flag = true;
//            mccType = list.get(position);
//        }
//        Log.d("convert", " onClick " + position + "  " + flag);
//        holder.rbtn.setChecked(flag);

        return convertView;
    }

    class ViewHolder {

        public TextView typeTv;
        public RadioButton rbtn;
    }

    public MccType getMccType() {
        return mccType;
    }

}
